<template>
  <el-dialog
    :title="!dataForm.orderId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="支付金额（分）" prop="totalFee">
      <el-input v-model="dataForm.totalFee" placeholder="支付金额（分）"></el-input>
    </el-form-item>
    <el-form-item label="用户ID" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="用户ID"></el-input>
    </el-form-item>
    <el-form-item label="微信交易号码" prop="transactionId">
      <el-input v-model="dataForm.transactionId" placeholder="微信交易号码"></el-input>
    </el-form-item>
    <el-form-item label="交易状态，1 未支付, 2已支付, 3 已退款, 4 支付错误, 5 已关闭" prop="status">
      <el-input v-model="dataForm.status" placeholder="交易状态，1 未支付, 2已支付, 3 已退款, 4 支付错误, 5 已关闭"></el-input>
    </el-form-item>
    <el-form-item label="支付方式，1 微信支付, 2 货到付款" prop="payType">
      <el-input v-model="dataForm.payType" placeholder="支付方式，1 微信支付, 2 货到付款"></el-input>
    </el-form-item>
    <el-form-item label="银行类型" prop="bankType">
      <el-input v-model="dataForm.bankType" placeholder="银行类型"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="支付时间" prop="payTime">
      <el-input v-model="dataForm.payTime" placeholder="支付时间"></el-input>
    </el-form-item>
    <el-form-item label="关闭时间" prop="closedTime">
      <el-input v-model="dataForm.closedTime" placeholder="关闭时间"></el-input>
    </el-form-item>
    <el-form-item label="退款时间" prop="refundTime">
      <el-input v-model="dataForm.refundTime" placeholder="退款时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          orderId: 0,
          totalFee: '',
          userId: '',
          transactionId: '',
          status: '',
          payType: '',
          bankType: '',
          createTime: '',
          payTime: '',
          closedTime: '',
          refundTime: ''
        },
        dataRule: {
          totalFee: [
            { required: true, message: '支付金额（分）不能为空', trigger: 'blur' }
          ],
          userId: [
            { required: true, message: '用户ID不能为空', trigger: 'blur' }
          ],
          transactionId: [
            { required: true, message: '微信交易号码不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '交易状态，1 未支付, 2已支付, 3 已退款, 4 支付错误, 5 已关闭不能为空', trigger: 'blur' }
          ],
          payType: [
            { required: true, message: '支付方式，1 微信支付, 2 货到付款不能为空', trigger: 'blur' }
          ],
          bankType: [
            { required: true, message: '银行类型不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          payTime: [
            { required: true, message: '支付时间不能为空', trigger: 'blur' }
          ],
          closedTime: [
            { required: true, message: '关闭时间不能为空', trigger: 'blur' }
          ],
          refundTime: [
            { required: true, message: '退款时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.orderId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.orderId) {
            this.$http({
              url: (`ricervcvcer-order/paylog/info/${this.dataForm.orderId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.totalFee = data.payLog.totalFee
                this.dataForm.userId = data.payLog.userId
                this.dataForm.transactionId = data.payLog.transactionId
                this.dataForm.status = data.payLog.status
                this.dataForm.payType = data.payLog.payType
                this.dataForm.bankType = data.payLog.bankType
                this.dataForm.createTime = data.payLog.createTime
                this.dataForm.payTime = data.payLog.payTime
                this.dataForm.closedTime = data.payLog.closedTime
                this.dataForm.refundTime = data.payLog.refundTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: `ricervcvcer-order/paylog/${!this.dataForm.orderId ? 'save' : 'update'}`,
              method: 'post',
              data: {
                'orderId': this.dataForm.orderId || undefined,
                'totalFee': this.dataForm.totalFee,
                'userId': this.dataForm.userId,
                'transactionId': this.dataForm.transactionId,
                'status': this.dataForm.status,
                'payType': this.dataForm.payType,
                'bankType': this.dataForm.bankType,
                'createTime': this.dataForm.createTime,
                'payTime': this.dataForm.payTime,
                'closedTime': this.dataForm.closedTime,
                'refundTime': this.dataForm.refundTime
              }
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
